<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left" @keyup.enter="handleLogin">

      <div class="title-container">
        <h3 class="title">{{ loginTitle }}</h3>
      </div>

      <el-form-item prop="userName">
        <el-input
          ref="userName"
          v-model="loginForm.userName"
          placeholder="用户名"
          prefix-icon="el-icon-user"
          name="userName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          prefix-icon="el-icon-lock"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <div class="captcha">
          <el-input
            ref="code"
            v-model="loginForm.code"
            prefix-icon="el-icon-message"
            placeholder="验证码"
            name="code"
            type="text"
            tabindex="3"
            maxlength="4"
            autocomplete="off"
          />
          <div class="imgs"><img @click="resetCaptcha" :src="captcha"></div>
        </div>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
    <div class="footer-page">
      <div>主办单位：酒泉市就业服务中心</div>
      <div><a href="https://beian.miit.gov.cn/" style="color: #fff" target="_blank">陇ICP备2023001144号-1</a></div>
      <div>Copyright@2022 酒泉市就业服务中心</div>
    </div>
  </div>
</template>

<script>
import getPageTitle from '@/utils/get-page-title'
import Cookies from "js-cookie";

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位数'))
      } else {
        callback()
      }
    }
    return {
      captcha:'',
      loginForm: {
        userName: '',
        password: '',
        code:'',
      },
      loginTitle: '',
      loginRules: {
        userName: [{ required: true,message: '请输入正确的用户名', trigger: 'blur'}],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, message: '请输入正确的验证码', trigger: 'blur'}]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.loginTitle = getPageTitle('')
    this.resetCaptcha();
  },
  methods: {
    resetCaptcha(){
      this.captcha=process.env.VUE_APP_BASE_API+'/kaptcha?'+Date.now()
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            console.log(this.redirect)
            let user = JSON.parse(Cookies.get("user"));
            console.log(user.userType)
            if(user.userType === 1)
            {
              this.$router.push({ path: '/dashboard' })
              this.$router.go(0);
            }else{
              this.$router.push({ path: '/questionnaireInvestigation/questionnaireInvestigation' })
            }

            this.loading = false
          }).catch((err) => {
            this.resetCaptcha();
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">

.footer-page{
  position: absolute;
  bottom: 0;
  width: 100%;
  background: #000000;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 120px;
  div{
    color: #fff;
    font-size: 15px;
    margin: 10px 0;
  }
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
.imgs {
  height: 40px;
  line-height: normal;
  img {
    height: 40px;
  }
}
.captcha {
  display: flex;
  align-items: flex-start;
}
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;

    input {
      display: flex;
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 0 5px 0 30px;
      color: #000;
      height: 40px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ccc;
    //background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  min-height: 100%;
  width: 100%;
  background: $bg url("https://job-1306862033.cos.ap-chengdu.myqcloud.com/admin%2Flogin%2F20221207093707.jpg") no-repeat center;
  background-size: 100% 100%;
  overflow: hidden;

  .login-form {
    position: absolute;
    transform: translate(-50%,-50%);
    left: 50%;
    top: 40%;
    width: 520px;
    padding: 100px 35px;
    padding-bottom: 50px;
    background:rgba(255,255,255,.95);
    border-radius: 15px;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #000;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
